package com.atgugui.eduservice.controller;


import com.atgugui.commonutils.Result;
import com.atgugui.eduservice.entity.EduTeacher;
import com.atgugui.eduservice.entity.vo.VoTeacherQuery;
import com.atgugui.eduservice.service.EduTeacherService;
import com.atgugui.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-19
 */
@Api(tags = "讲师管理模块")
@RestController // 组合注解：@Controller + @Responseable   该Controller下所有返回到页面的都是json数据
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 测试代码：查询所有讲师信息
     */
    @ApiOperation(value = "查询所有讲师信息")
    @GetMapping("/findAll")
    public Result findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return Result.ok().data("items",list);
    }

    /**
     * 讲师信息的删除  通过id删除
     */
    @ApiOperation(value = "按照id删除讲师信息")
    @DeleteMapping("{id}")
    public Result removeTeacher(@PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        return flag ? Result.ok() : Result.error();
    }

    /**
     * 讲师信息的分页查询  无条件查询
     */
    @ApiOperation(value = "讲师分页查询-无条件查询")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public Result pageListTeacher(@PathVariable Long current, @PathVariable Long limit) {
        Page<EduTeacher> pageTeacher = new Page<>(current,limit); // 创建分页对象
        eduTeacherService.page(pageTeacher,null); // 分页
        long total = pageTeacher.getTotal(); // 获取当前页的总数据条数
        List<EduTeacher> records = pageTeacher.getRecords(); // 实际数据
        return Result.ok().data("total",total).data("items",records);
    }

    /**
     * 讲师信息的分页查询  条件查询   注意如果是将数据请求体中的JSON数据封装到VO实体中
     * 此时的请求应该是POST请求，GET请求无法获取
     */
    @ApiOperation(value = "讲师信息的分页查询-条件查询")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public Result pageListTeacherCondition(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) VoTeacherQuery voTeacherQuery) {
        Page<EduTeacher> page = new Page<>(current,limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = voTeacherQuery.getName();
        String begin = voTeacherQuery.getBegin();
        String end = voTeacherQuery.getEnd();
        Integer level = voTeacherQuery.getLevel();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_modified",end);
        }
        queryWrapper.orderByDesc("gmt_create");
        eduTeacherService.page(page,queryWrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return Result.ok().data("total",total).data("items",records);
    }

    /**
     * 添加讲师信息接口
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody(required = false) EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? Result.ok() : Result.error();
    }

    /**
     * 根据id查询讲师信息
     */
    @ApiOperation(value = "根据id查询讲师信息")
    @GetMapping("/getTeacher/{id}")
    public Result getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return Result.ok().data("teacher",eduTeacher);
    }

    /**
     * 更新讲师信息
     */
    @ApiOperation(value = "更新讲师信息")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody(required = false) EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        return flag ? Result.ok() : Result.error();
    }
}

